package org.compression.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import redis.clients.jedis.Jedis;
public class RedisTestStream {
	Jedis redis = new Jedis("127.0.0.1",6379);

	// 序列化方法
	public byte[] object2Bytes(Object value) {
		if (value == null)
			return null;
		ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream outputStream;
		try {
			outputStream = new ObjectOutputStream(arrayOutputStream);
			outputStream.writeObject(value);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				arrayOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return arrayOutputStream.toByteArray();
	}

	// 反序列化方法
	public Object byte2Object(byte[] bytes) {
		if (bytes == null || bytes.length == 0)
			return null;
		try {
			ObjectInputStream inputStream;
			inputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
			Object obj = inputStream.readObject();
			return obj;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 保存文件方法
	public void setFile(String key, String path) {

		File fr = new File(path);
		redis.set(key.getBytes(), object2Bytes(fr));
	}

	// 读取文件对象方法
	public File getFile(String key) {
		Jedis redis = new Jedis("localhost",6379);
		File file = (File) byte2Object(redis.get(key.getBytes()));
		return file;
	}

	public void  testFile(String key, String path) throws Exception {
		//setFile("testStream", "D:\\testFile\\ifeng.mht");
	
		setFile(key, path);
//		return file;
	//	File file = getFile(key);
	//	File file2 = new File("C:\\Users\\Administrator\\Desktop\\asd.jpg");
		
		
		
//		file2.createNewFile();
	//	cutFile(file,file2);
//		BufferedReader br = new BufferedReader(new FileReader(file));
//		String record = null;
//		while ((record = br.readLine()) != null) {
//			System.out.println("record:" + record);
//		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//测试联通否
//		Jedis redis = new Jedis("119.XXX.XXX.XXX",6379);
//		System.out.println("Server is running: " + redis.ping());
	}

	 public static void cutFile(File file1, File file2){  
	        FileOutputStream fileOutputStream = null;  
	        InputStream inputStream = null;  
	        byte[] bytes = new byte[1024];  
	        int temp = 0;  
	        try {  
	            inputStream = new FileInputStream(file1);  
	            fileOutputStream = new FileOutputStream(file2);  
	            while((temp = inputStream.read(bytes)) != -1){  
	                fileOutputStream.write(bytes, 0, temp);  
	                fileOutputStream.flush();  
	            }  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        }catch (IOException e) {  
	            e.printStackTrace();  
	        }finally{  
	            if (inputStream != null) {  
	                try {  
	                    inputStream.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	            if (fileOutputStream != null) {  
	                try {  
	                    fileOutputStream.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	        }  
	          
	    }  
}
